<?php
namespace Yousheng\DatabaseToolbox\Console;
use Illuminate\Support\Facades\DB;

trait TraitTable
{
    protected static $templateGetTables = "SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_TYPE='BASE TABLE' AND TABLE_SCHEMA='%s'";


    /**
     * 从数据库中读取所有的表
     *
     * @return mixed
     */
    public function tables($databaseName)
    {
        try {
            // 切换到指定数据库
            DB::unprepared("USE `{$databaseName}`");

            // 使用SHOW TABLES命令
            $tables = DB::select('SHOW TABLES');

            $this->info("Tables in database '{$databaseName}':");
            foreach ($tables as $table) {
                // SHOW TABLES 返回的结果第一列名为 "Tables_in_{数据库名}"
                $tableName = array_values((array) $table)[0];
                $this->line($tableName);
            }

        } catch (\Exception $e) {
            $this->error("Error: {$e->getMessage()}");
        }
        dd('33333333');


        dd(DB::connection($databaseName)->select('SHOW TABLES'));
        $sql="SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_TYPE='BASE TABLE' AND TABLE_SCHEMA='demo_bemusic'";
//        return DB::select(sprintf(static::$templateGetTables, $databaseName));
        echo "123";
dd( DB::select($sql));
    }


    /**
     * @return mixed
     */
    public function getTables()
    {
        return $this->schema->listTableNames();
    }

    public function getFields($table)
    {
        return $this->fieldGenerator->generate($table, $this->schema, $this->database, $this->ignoreIndexNames);
    }

    public function getForeignKeyConstraints($table)
    {
        return $this->foreignKeyGenerator->generate($table, $this->schema, $this->ignoreForeignKeyNames);
    }
}